package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.produce.utils.ParseJson;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetProductOrderListModel implements IGetProductOrderListModel {

    @Override
    public void getReceiveEngineeringOrderList(final OnGetProductOrderListListener onGetProductOrderListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ProjectUtil.GET_PRODUCT_RECEIVE_ORDER_LIST);
                    HttpResponse response;
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    response = client.execute(httpPost);

                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    Log.e("GetProductOrderListMode", "result=" + result);

                    if (null != onGetProductOrderListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("productOrderResponseList"));
                            if (jsonArr != null) {
                                List<ProduceProduceOrderBean> produceOrderBeanList_status_watting_receive = new ArrayList<ProduceProduceOrderBean>();
                                List<ProduceProduceOrderBean> produceOrderBeanList_status_received = new ArrayList<ProduceProduceOrderBean>();
                                List<ProduceProduceOrderBean> produceOrderBeanList_status_returns = new ArrayList<ProduceProduceOrderBean>();
                                List<ProduceProduceOrderBean> produceOrderBeanList_status_success = new ArrayList<ProduceProduceOrderBean>();
                                List<ProduceProduceOrderBean> produceOrderBeanList = new ArrayList<ProduceProduceOrderBean>();
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        ProduceProduceOrderBean orderBean = new ProduceProduceOrderBean();
                                        orderBean.setOrderId(jsonObj.getString("orderId"));
                                        orderBean.setEngineeringName(jsonObj.getString("engineeringName"));

                                        String isHurry = jsonObj.getString("isHurry");
                                        if (null != isHurry) {
                                            if (isHurry.equals("HURRY")) {
                                                orderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.HURRY);
                                            } else if (isHurry.equals("NOT_HURRY")) {
                                                orderBean.setIsHurry(CommonEnum.ProduceOrderIsHurry.NOT_HURRY);
                                            }
                                        }

                                        CommonEnum.ProduceOrderLevel level= ParseJson.parseProdutOrderLevel(jsonObj,"level");
                                        orderBean.setLevel(level);

                                        String status = jsonObj.getString("status");
                                        if (null != status) {
                                            if (status.equals("CREATED")) {
                                                orderBean.setStatus(CommonEnum.ProduceOrderStatus.CREATED);
                                            } else if (status.equals("SEND")) {
                                                orderBean.setStatus(CommonEnum.ProduceOrderStatus.SEND);
                                                produceOrderBeanList_status_watting_receive.add(orderBean);
                                            } else if (status.equals("RECEIVED")) {
                                                orderBean.setStatus(CommonEnum.ProduceOrderStatus.RECEIVED);
                                                produceOrderBeanList_status_received.add(orderBean);
                                            } else if (status.equals("RETURNED")) {
                                                orderBean.setStatus(CommonEnum.ProduceOrderStatus.RETURNED);
                                                produceOrderBeanList_status_returns.add(orderBean);
                                            } else if (status.equals("COMPLETED")) {
                                                orderBean.setStatus(CommonEnum.ProduceOrderStatus.COMPLETED);
                                                produceOrderBeanList_status_success.add(orderBean);
                                            }
                                        }

                                        orderBean.setProjectName(jsonObj.getString("projectName"));
                                        orderBean.setType(jsonObj.getString("type"));

                                        produceOrderBeanList.add(orderBean);

                                    }
                                }
                                onGetProductOrderListListener.getDataSuccessed(produceOrderBeanList, produceOrderBeanList_status_watting_receive, produceOrderBeanList_status_received, produceOrderBeanList_status_returns, produceOrderBeanList_status_success);
                            }
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            onGetProductOrderListListener.getDataFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnGetProductOrderListListener {

        void getDataSuccessed(List<ProduceProduceOrderBean> orderParentBeanList,
                              List<ProduceProduceOrderBean> productBeanList_status_send,
                              List<ProduceProduceOrderBean> productBeanList_status_receives,
                              List<ProduceProduceOrderBean> productBeanList_status_returns,
                              List<ProduceProduceOrderBean> productBeanList_status_success);

        void getDataFailed(String desc);
    }
}
